package com.ravi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ravi.common.R;
import com.ravi.core.patient.BasePatient;
import com.ravi.core.patient.NormalPatient;
import com.ravi.core.patient.VipPatient;
import com.ravi.core.queue.CheckUpQueue;
import com.ravi.core.queue.HashQueueManager;
import com.ravi.dao.CheckRoomDAO;
import com.ravi.dao.PatientQueueInfoDAO;
import com.ravi.entity.CheckPlan;
import com.ravi.entity.CheckRoom;
import com.ravi.entity.PatientQueueInfo;
import com.ravi.service.CheckPlanService;
import com.ravi.service.PatientQueueInfoService;
import com.ravi.service.QueueService;
import com.ravi.vo.PlanItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("test")
@Slf4j
public class TestController {
	@Autowired
	private CheckPlanService checkPlanService;

	@Autowired
	private PatientQueueInfoService patientService;

	@Autowired
	private PatientQueueInfoDAO patientQueueInfoDAO;

	@Autowired
	private CheckRoomDAO checkRoomDAO;

	@GetMapping("hello")
	public R hello() {
		R result = R.ok();
		result.put("username", "ravi");
		result.put("role", "admin");
		return result;
	}

	@PostMapping("findAllPlans")
	public PageInfo<CheckPlan> findAll(@RequestParam Integer pageNum,
									   @RequestParam Integer pageSize) {
		return checkPlanService.listAllPlans(pageNum, pageSize);
	}

	@GetMapping("planItems")
	public PlanItemVo listItems(Integer planId) {
		return checkPlanService.listItemListForOnePlan(planId);
	}


	@GetMapping("patients/{pageNum}/{pageSize}")
	public R getPatientsList(@PathVariable String pageNum,
							 @PathVariable String pageSize) {
		int page_num = Integer.parseInt(pageNum);
		int page_size = Integer.parseInt(pageSize);
		PageInfo<PatientQueueInfo> list = patientService.getPatientQueueInfoList(page_num, page_size);
		if (list.getList().isEmpty()) {
			return R.error(404, "找不到您所查询的数据！");
		}
		return R.ok().put("data", list);
	}

	@GetMapping("new")
	public R testQueue() {
		CheckRoom checkRoom = checkRoomDAO.selectCheckRoomById(23);
		CheckUpQueue queue = new CheckUpQueue(checkRoom);
		PatientQueueInfo patientInfo = patientQueueInfoDAO.selectPatientQueueInfoByPatientId(1098);
		BasePatient patient = null;
		if (patientInfo.getLevel() == 1) {
			patient = new VipPatient(patientInfo);
		}else {
			patient = new NormalPatient(patientInfo);
		}
		queue.add(patient);
		return R.ok().put("queue", queue);
	}

	@Autowired
	private QueueService queueService;


	@PostMapping("enqueue")
	public R testEnqueue(@RequestParam Integer patientId) {
		PatientQueueInfo patientInfo = patientService.getPatientInfoById(patientId);
		Integer queueId = patientInfo.getQueueId();
		// 省略enqueue校验
		queueService.enqueue(patientInfo);
		return R.ok("入队成功!");
	}

	@GetMapping("queue")
	public R testGetQueue(@RequestParam Integer queueId) {
		// 省略校验
		CheckUpQueue queue = HashQueueManager.queueManager.get(queueId);
		return R.ok().put("queue", queue);
	}

}
